import http.client, urllib.request, urllib.parse, urllib.error, base64
import json
import pymysql
import datetime

#专家信息补齐，下载图片

def getImages(db):
    try:
        cursor = db.cursor()
        cursor.execute("SELECT DISTINCT t.url_picture,t.name from pic_table t where t.url_picture is not null")
        results = cursor.fetchall()

        i=0
        for row in results:
            i+=1
            print(u'正在提取第%s个专家,专家pId为%s' % (i,row[1]))
            imgPath="https://scholar.google.com"+row[0]
            imgName=row[1]+".jpeg"
            urllib.request.urlretrieve(imgPath,'D:/Images/%s' % imgName)
    except Exception as e:
        print("[Errno {0}] {1}".format(e.errno, e.strerror))
    finally:
        print("over")

if __name__ == "__main__":  # 起到一个初始化或者调用函数的作用
    # db = pymysql.connect("localhost:3398", "root", "123456", "dataService", charset='utf8')
    db = pymysql.Connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        passwd='123456',
        db='dataservice',
        charset='utf8'
    )
    cursor = db.cursor()
    # prem(db)
    getImages(db)
    cursor.close()